import itsdangerous
from django.conf import settings


class Token:
    setup = itsdangerous.TimedJSONWebSignatureSerializer(
        settings.SECRET_KEY,
        # 有效期单位S
        expires_in=60 * 60 * 1
    )

    # 加密
    @classmethod
    def create_token(cls, data):
        token = cls.setup.dumps(data).decode()
        return token

    # 解码
    @classmethod
    def check_token(cls, token):
        try:
            data = cls.setup.loads(token)
        except:
            return False
        return data
